package Others;

public class _482_LicenseKeyFormatting {
    public String licenseKeyFormatting(String S, int K) {
        StringBuffer result = new StringBuffer();
        int count = 0;
        for (int i = S.length() - 1; i >= 0; i--) {
            char c = S.charAt(i);
            if (c != '-') {
                if (count < K) {
                    count++;
                } else {
                    count = 1;
                    result.append('-');
                }
                result.append(Character.toUpperCase(c));
            }
        }
        return result.reverse().toString();
    }
}
